package kr.shincooper.funnyhome.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kr.shincooper.funnyhome.common.EncryptUtil;
import kr.shincooper.funnyhome.dao.MemberDao;
import kr.shincooper.funnyhome.model.MemberModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Funny Home의 Member를 관리하는 서비스 클래스.
 * 
 * @author bluemirr5
 *
 */
@Service
public class MemberServiceImpl implements MemberService {
	@Autowired
	private MemberDao memberDao;

	/* (non-Javadoc)
	 * @see kr.shincooper.funnyhome.service.MemberService#getMember(int)
	 */
	@Override
	public MemberModel getMember(String memberId) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("memberId", memberId);
		MemberModel memberModel = memberDao.getMember(param);
		
		if(memberModel == null) {
			throw new Exception("");
		}
		return memberModel;
	}

	/* (non-Javadoc)
	 * @see kr.shincooper.funnyhome.service.MemberService#getMember()
	 */
	@Override
	public List<MemberModel> getMember() throws Exception {
		List<MemberModel> memberModelList = memberDao.getMember();
		return memberModelList;
	}

	/* (non-Javadoc)
	 * @see kr.shincooper.funnyhome.service.MemberService#login(java.lang.String, java.lang.String)
	 */
	@Override
	public String login(String id, String pass) throws Exception {
		MemberModel memberModel = getMember(id);
		EncryptUtil encrypt = new EncryptUtil(id);
		String encryptedPass = encrypt.encrypt(pass);
		if(memberModel.getMemberPassword().equalsIgnoreCase(encryptedPass)) {
			//TODO Redis등에 저장한다.
			return EncryptUtil.getRandomString();
		}
		return null;
	}

}
